<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>题库管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>组件</cite></a>
    <a><cite>数据表格</cite></a>
    <a><cite>题库管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-header">题库管理</div>
        <div class="layui-card-body">

          <div class="test-table-reload-btn layui-form-item" style="margin-bottom: 10px;">
            培训名称：
            <div class="layui-inline">
              <input class="layui-input" name="trainName" id="test-table-demoReload" autocomplete="off">
            </div>
            培训状态：
            <div class="layui-inline layui-form">
              <select name="trainStatus" id="form-isPublic">
                <option value="">--请选择--</option>
                <option value="0">未开始</option>
                <option value="1">进行中</option>
                <option value="2">已结束</option>
              </select>
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
          </div>

          <table class="layui-hide" id="table-train" lay-filter="table-train-toolbar"></table>

          <script type="text/html" id="table-train-trainStatus">
            {{#  if(d.trainStatus == 0){ }}
            <span class="layui-badge layui-bg-orange">未开始</span>
            {{#  } else if(d.trainStatus == 1){ }}
            <span class="layui-badge layui-bg-blue">进行中</span>
            {{#  } else{ }}
            <span class="layui-badge layui-bg-gray">已结束</span>
            {{#  } }}
          </script>

          <script type="text/html" id="table-train-trainImg">
            <a href="{{ d.trainImg }}" target="_blank"><img style="display: inline-block; width: 50%; height: 100%;" src="{{ d.trainImg }}"></a>
          </script>

          <script type="text/html" id="table-train-toolbar">
            <div class="layui-btn-container">
              <button class="layui-btn layui-btn-sm" lay-event="add">发布培训</button>
            </div>
          </script>

          <script type="text/html" id="table-train-list">
            <a class="layui-btn layui-btn-xs" lay-event="qrcode"><i class="layui-icon layui-icon-search"></i>查看二维码</a>
            <a class="layui-btn layui-btn-xs" lay-event="search"><i class="layui-icon layui-icon-search"></i>培训结果</a>
            {{#  if(d.trainStatus == 0){ }}
            <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            {{#  } else{ }}
            <a class="layui-btn layui-btn-xs layui-btn-disabled"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled"><i class="layui-icon layui-icon-delete"></i>删除</a>
            {{#  } }}
          </script>

          <script type="text/html" id="table-train-accuracy">
            {{d.accuracy}}%
          </script>

          <script type="text/html" id="table-train-pass">
            {{d.pass}}%
          </script>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    editData = [];
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var admin = layui.admin
            ,table = layui.table
            ,$ = layui.$
            ,form = layui.form;

        table.render({
            elem: '#table-train'
            ,url: '/admin/train/trains'
            ,page: true
            ,limit : 10
            ,limits: [10,30,60,90]
            ,loading: true   //开启翻页加载动画
            ,toolbar: '#table-train-toolbar'   //开启toolbar
            //,height: 'full-100' //高度最大自适应
            ,cols: [[
                {field:'trainId',  title: 'ID', sort: true ,hide:true}
                ,{field:'trainContent',  title: '培训名称'}
                ,{field:'trainStart',  title: '开始时间',sort: true}
                ,{field:'trainEnd',  title: '结束时间',sort: true}
                ,{field:'trainTime',  title: '考试时长',hide:true}
                ,{field:'trainNum',  title: '题目数量',hide:true}
                ,{field:'trainBanks',  title: '所选题库',hide:true}
                ,{field:'trainStatus',  title: '培训状态',templet: '#table-train-trainStatus',unresize: true,align:"center"}
                ,{field:'trainUrl',  title: '二维码信息',hide:true}
                ,{field:'trainImg',  title: '二维码图片',templet: '#table-train-trainImg'}
                ,{field:'accuracy', title: '正确率',templet: '#table-train-accuracy'}
                ,{field:'pass', title: '通过率',templet: '#table-train-pass'}
                ,{field:'createTime',  title: '发布日期',sort: true,hide:true}
                ,{title: '操作',templet: '#table-train-list',width:380, unresize: true,fixed:"right",align:"center"}

            ]]

        });

        //头工具栏事件
        table.on('toolbar(table-train-toolbar)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            if(obj.event === 'add'){
                trainAdd();
            }
        });

        //监听行工具事件
        table.on('tool(table-train-toolbar)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('删除后数据不可恢复，确定要删除么？', function(index){
                    console.log(data.bankId);
                    $.ajax({
                        url:"/admin/train/train/"+data.trainId,
                        async:false,
                        method:"delete",
                        dataType:"json"
                    });
                    table.reload('table-train');
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                editData = data;
                trainEdit();
            } else if(obj.event === 'search'){
                editData = data;
                trainSearch();
            } else if(obj.event === 'qrcode'){
                console.log(data.trainImg);
                window.open(data.trainImg,'_blank');
            }
        });

        //监听公开操作
        form.on('switch(test-table-public)', function(obj){
            layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
        });

        //搜索
        var active = {
            reload: function(){
                //执行重载
                table.reload('table-train', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        trainName: $('[name=trainName]').val(),
                        trainStatus: $('[name=trainStatus]').val()
                    }
                });
            }
        };

        $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

    function trainAdd(){
        var index = layer.open({
            type: 2
            ,title: '发布培训'
            ,content: '/layuiadmin/add/train-add.html'
            ,maxmin: true
            ,area: ['550px', '450px']
            ,btn: ['发布', '取消']
            ,yes: function(index, layero){
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            }
        });
        layer.full(index);
    }

    function trainEdit(){
        var index = layer.open({
            type: 2
            ,title: '修改题库'
            ,content: '/layuiadmin/edit/train-edit.html'
            ,maxmin: true
            ,area: ['550px', '450px']
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                submit.click();
            }
        });
        layer.full(index);
    }

    function trainSearch(){
        layer.open({
            type: 2
            ,title: '培训结果'
            ,content: '/layuiadmin/add/train-result.html'
            ,maxmin: true
            ,area: ['980px', '100%']
        });
    }
</script>
</body>
</html>